package com.xunshangwang.pinhaohuo.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.xunshangwang.pinhaohuo.tools.PayHelper;

/**
 * Created by xzp on 15/8/24.
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{
    IWXAPI mIwxapi;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mIwxapi= WXAPIFactory.createWXAPI(this, PayHelper.WX_APP_ID);
        mIwxapi.handleIntent(getIntent(),this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mIwxapi.handleIntent(intent,this);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        if(baseResp.getType()== ConstantsAPI.COMMAND_PAY_BY_WX)
        {
            if(PayHelper.getCallBack()!=null) {
                switch (baseResp.errCode){
                    case 0:
                        PayHelper.getCallBack().onPayComplete(PayHelper.WAY_WECHAT,PayHelper.PAY_SUCCESS,"支付成功");
                        break;
                    case -1:
                        PayHelper.getCallBack().onPayComplete(PayHelper.WAY_WECHAT,PayHelper.PAY_FAIL,"支付失败");
                        break;
                    case -2:
                        PayHelper.getCallBack().onPayComplete(PayHelper.WAY_WECHAT,PayHelper.PAY_CANCEL,null);
                        break;
                    default:
                        PayHelper.getCallBack().onPayComplete(PayHelper.WAY_WECHAT,PayHelper.PAY_OTHER,"未知支付结果,请等待确认");
                        break;
                }
                PayHelper.unRegisterCallback();
            }
        }
        finish();
    }
}
